﻿/*
 * Created by SharpDevelop.
 * User: William
 * Date: 28/10/2011
 * Time: 08:45 p.m.
 * 
 */
using System;
using System.Data;
using MySql.Data.MySqlClient;

namespace Principal
{
    /// <summary>
    /// Description of DatabaseManager.
    /// </summary>
    public sealed class DatabaseManager
    {
        private static DatabaseManager _instance = new DatabaseManager();
        
        public static DatabaseManager Instance { get { return _instance; } }
        
        private String _connectionString = "";
        private MySqlConnection _conn;
        
        private TipoAtencionDB _tipoAtencion;
        
        public TipoAtencionDB TipoAtencion  { get { return this._tipoAtencion; } }

        private DatabaseManager()
        {
            if (this._conn == null)
            {
                CreateConnection(Environment.CONNECTION_STRING);
            }
            /* Creamos las instancias de los objetos de la BD */
            this._tipoAtencion = new TipoAtencionDB(this._conn);
            
        }
        
        public void CreateConnection(String connectionString)
        {
            this._connectionString = connectionString;
            try
            {
                if (_conn == null)
                {
                    _conn = new MySqlConnection(connectionString);
                }
            }
            catch 
            {
                /* No se pudo conectar por x razon... */
                _conn = null;
            }
            
        }
        public DataTable getUsuarios()
        {
            try
            {
                string query = "SELECT * FROM Usuario";
                MySqlDataAdapter adapter = new MySqlDataAdapter(query, this._conn);
                DataSet ds = new DataSet();
                adapter.Fill(ds);
                return ds.Tables[0];
            }
            catch 
            {
                return null;
            }
        }
    }
}
